# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=szip
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1
pkgrel=4
pkgdesc="Extended-Rice lossless compression algorithm implementation (mingw-w64)"
arch=('any')
url="https://www.hdfgroup.org/doc_resource/SZIP/"
source=("https://www.hdfgroup.org/ftp/lib-external/szip/${pkgver}/src/szip-${pkgver}.tar.gz"
        "szip-hdf5.patch"
        "szip-default-import-suffix.patch"
        "szip-fix-install-docs.patch"
        "szip-fix-cmake-macros.patch"
        "szip-proper-library-names-mingw.patch")
sha256sums=('a816d95d5662e8279625abdbea7d0e62157d7d1f028020b1075500bf483ed5ef'
            '468817f23d46044c7e3ae3db7547a939fc2607242445e35ae85876244a77df35'
            'd3514729d82d0427a03681a637a9703be01045dec15267556ca9c912419c1c5a'
            '7d8df352ac5da784237b8c3d8ae9e26d60356acaafd616a208cc441e12267744'
            '924f1991443d112ec2bc024977ae0af44403838cb5bab398c0ecadf442002b32'
            'e3de996fa3c2ad98be70695a9883b5cb5c3c332e505eeb778dfd7e51f69471d7')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p0 -i ${srcdir}/szip-hdf5.patch
  patch -p1 -i ${srcdir}/szip-default-import-suffix.patch
  patch -p1 -i ${srcdir}/szip-fix-install-docs.patch
  patch -p1 -i ${srcdir}/szip-fix-cmake-macros.patch
  patch -p1 -i ${srcdir}/szip-proper-library-names-mingw.patch
}

build() {
  # Build static version
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-static-${MINGW_CHOST} && cd ${srcdir}/build-static-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${pkgver}
  make

  # Build shared version
  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-shared-${MINGW_CHOST} && cd ${srcdir}/build-shared-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
  ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/build-static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/build-shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
